// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import FSM from "./lib/FSM";
import FSMConditionBool, { BoolCondition } from "./lib/FSMConditionBool";
import PlayerBlackboard from "./PlayerBlackboard";
import PlayerIdleState from "./PlayerIdleState";
import PlayerWalkState from "./PlayerWalkState";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FSMPlayer extends cc.Component {
    private _fsm: FSM;
    protected onLoad(): void {
        this._fsm = new FSM();
        this._fsm.node = this.node;
        this._fsm.init(new PlayerBlackboard());

        let idle = this._fsm.addState(new PlayerIdleState());
        let walk = this._fsm.addState(new PlayerWalkState());

        let transition = idle.addTransition(walk.id);
        transition.addCondition(new FSMConditionBool("isKeyPressed", null, BoolCondition.TRUE));

        transition = walk.addTransition(idle.id);
        transition.addCondition(new FSMConditionBool("isKeyPressed", null, BoolCondition.FALSE));
    }

    protected update(dt: number): void {
        this._fsm.update(dt);
    }
}